<?php
/**
 * Created by PhpStorm.
 * User: Zwc
 * Date: 2025/11/12
 * Time: 15:13
 */

namespace app\admin\service;

use app\admin\model\LangLanguageModel;
use think\facade\Log;

class LangLanguageService
{
    /**
     * 获取数据
     * @param $params
     * @return \think\Paginator
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getData($params): \think\Paginator
    {
        $condition = [
            ['del_flag', '=', 0],
        ];
        if (!empty($params['langId'])) {
            $condition[] = ['lang_id', '=', $params['langId']];
        }
        $model = new LangLanguageModel();
        $data = $model->where($condition)->order('id desc')->paginate(20);

        $langData = CountryService::getRecommendLanguage();
        $langMap = array_column($langData, 'name', 'id');

        foreach ($data as &$item) {

            if (isset($langMap[$item['language_id']])) {
                $item['languageName'] = $langMap[$item['language_id']];
            }else {
                $item['languageName'] = '';
            }
        }
        return $data;
    }

    public function add($params)
    {
        $condition = [
            ['del_flag', '=', 0],
            ['language_id', '=', $params['language_id']],
            ['lang_id', '=', $params['lang_id']],
        ];
        $model = new LangLanguageModel();
        $data = $model->where($condition)->find();
        if ($data) {
            return ['code' => -1,'msg' => '语言已经添加过了~请勿重复添加'];
        }

        $insertData = [
            'name' => $params['name'],
            'language_id' => $params['language_id'],
            'lang_id' => $params['lang_id'],
        ];

        try {
            $model->insert($insertData);
            return ['code' => 0, 'msg' => '操作成功'];
        }catch (\Exception $e) {
            Log::error($e->getMessage());
            Log::error($e->getTraceAsString());
            return ['code' => 0, 'msg' => '操作失败，请联系管理员'];
        }
    }

    public function detail($id)
    {
        $model = new LangLanguageModel();
        return $model->find($id);
    }

    public function modify($params)
    {
        $condition = [
            ['del_flag', '=', 0],
            ['language_id', '=', $params['language_id']],
            ['lang_id', '=', $params['lang_id']],
            ['id', '<>', $params['id']],
        ];
        $model = new LangLanguageModel();
        $data = $model->where($condition)->find();
        if ($data) {
            return ['code' => -1,'msg' => '语言已经添加过了~请勿重复添加'];
        }

        $insertData = [
            'name' => $params['name'],
            'language_id' => $params['language_id'],
            'lang_id' => $params['lang_id'],
        ];

        try {
            $model->where(['id' => $params['id']])->data($insertData)->save();
            return ['code' => 0, 'msg' => '操作成功'];
        }catch (\Exception $e) {
            Log::error($e->getMessage());
            Log::error($e->getTraceAsString());
            return ['code' => 0, 'msg' => '操作失败，请联系管理员'];
        }
    }

    public function del($params)
    {

    }


}